<template>
    <div>
        <div class="header-title">
            <span @click="$router.go(-1)" class="mint-toast-icon mintui mintui-back"></span>
            <span v-show="titleType==1?true:false">找回密码</span>
            <span v-show="titleType==2?true:false">设置密码</span>
            <span></span>
        </div>
        <div class="num-list">
            <div class="input-area">
                <span>设置密码</span>
                <input v-model="password.password" style="text-align:right" type="text" name="" id="" placeholder="请输入登录密码">
            </div>
            <div class="input-area">
                <span>确认密码</span>
                <input v-model="password.passwordAgain" style="text-align:right" type="text" name="" id="" placeholder="请再次输入登录密码">
            </div>
            <mt-button
                size="normal"
                class="submit-btn"
                @click="modifyLogin"
            >确定</mt-button>
        </div>
    </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
    data(){
        return{
            titleType:this.$route.query.titleType,//标题切换
            modifyType:this.$route.query.modifyType,//确定修改登录密码的方式
            verifyCode:this.$route.query.verifyCode,//所得验证码
            account:this.$route.query.account,//账号
            password:{
                password:'',
                passwordAgain:'',
            },
            token:''
        }
    },
    created(){
        if(!JSON.parse(this.$store.state.userMsg)){
            this.token = ''
        }else{
            this.token = JSON.parse(this.$store.state.userMsg).token
        }
    },
    methods:{
        modifyLogin(){
            var checkPassword = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)^.{8,16}$/;
            if(this.password.password==''){
                Toast({ message: "密码不能为空！" });
                return false;
            }
            if(!checkPassword.test(this.password.password)){
                Toast({ message: "请输入8-16位数的密码,不能为纯英文、数字、符号！" });
                return false;
            }
            if(this.password.password != this.password.passwordAgain){
                Toast({ message: "输入的密码不一致！" });
                return false;
            }
            if(this.titleType == 1){
                 if(this.modifyType == 'phone'){
                var params = {
                    phoneNo:this.account,
                    newPassword:this.password.password,
                    confirmPassword:this.password.passwordAgain,
                    verificationCode:this.verifyCode,
                    type:'0'
                }
                this.modifyLoginPort(params)
            }else if(this.modifyType == 'email'){
                var params = {
                    mailbox:this.account,
                    newPassword:this.password.password,
                    confirmPassword:this.password.passwordAgain,
                    verificationCode:this.verifyCode,
                    type:'1'
                }
                this.modifyLoginPort(params)
            }
            }else if(this.titleType == 2){
                this.setLoginPort();
            }
           
        },
        //修改登录密码
        modifyLoginPort(params){
            this.$axios.post(this.address.forgetPassword,params).then(res => {
                console.log(res.data)
                if(res.data.code == 200){
                    Toast({ message: "修改成功！" });
                    this.$router.push({name:'login'})
                }else{
                    Toast({ message: res.data.msg });
                }
            })
        },
        //设置登录密码
        setLoginPort(){
            var params = {
                token:this.token,
                newPassword:this.password.password,
                confirmPassword:this.password.passwordAgain,
                type:'1'
            }
            this.$axios.post(this.address.setTransactionPassword,params).then(res => {
                if(res.data.code == 200){
                    Toast({ message: "修改成功！" });
                    this.$router.push({name:'login'})
                }else{
                    Toast({ message: res.data.msg });
                }
            })
        }
    }
};
</script>
<style lang="less" scoped>
.num-list {
    padding: 15px;
    box-sizing: border-box;
}
</style>
